package com.beta.pay.yeepay.core;

import com.beta.pay.yeepay.response.RefundResponse;
import com.beta.pay.yeepay.request.RefundRequest;
import com.beta.pay.yeepay.utils.ZGTUtils;
import org.apache.commons.collections.MapUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 分账组件
 * Created by vector on 2016/11/30.
 */
public class Refund extends Component {

    private static final String API_URL = "https://o2o.yeepay.com/zgt-api/api/refund";

//    private final Logger logger = LoggerFactory.getLogger(getClass());

    protected Refund(Yeepay yeepay) {
        super(yeepay);
    }

    @Override
    protected String getComponentName() {
        return "退款";
    }

    public RefundResponse refund(RefundRequest request) {
        Map<String, String> responseData = fetchData(API_URL, toParamMap(request),
                ZGTUtils.REFUNDAPI_REQUEST_HMAC_ORDER, ZGTUtils.REFUNDAPI_RESPONSE_HMAC_ORDER);
        return buildRefundResponse(responseData);
    }

    private RefundResponse buildRefundResponse(Map<String, String> responseData) {
        RefundResponse response = new RefundResponse();
        response.setCustomerNumber(MapUtils.getString(responseData, "customernumber"));
        response.setRequestId(MapUtils.getString(responseData, "requestid"));
        response.setCode(MapUtils.getString(responseData, "code"));
        response.setRefundExternalId(MapUtils.getString(responseData, "refundexternalid"));
        return response;
    }

    private Map<String, String> toParamMap(RefundRequest request) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("requestid", request.getRequestId());
        paramMap.put("orderrequestid", request.getOrderRequestId());
        paramMap.put("amount", request.getAmount().toString());
        paramMap.put("divideinfo", request.divideInfoString());
        paramMap.put("confirm", request.getConfirm());
        paramMap.put("memo", request.getMemo());
        return paramMap;
    }

}
